/**----------------------------------------------------------*/
/** JS:  cmi_ad_loc_list.js（单页管理--单页类型）           */
/**----------------------------------------------------------*/

var tipc = 'AD_LOC';
var win = null;

var add = function() {
	try {
		var div = getEditDiv(null);
		
		win = MyUtils._getToolbarWin("dataPanel", "editform", div, submitAdd, panel + "--添加","保存提交");
		win.window({
			width:700,
			height:300,
			modal: true
		});
	
		repainLoc(null);
	} catch (e) {
		showJsException(e);
	}
};

var modify = function() {
	try{
		var record = datagridOneRow('dataPanel');
		
		if(record == null) {
			return;
		}
		var div = getEditDiv(record);
		win = MyUtils._getToolbarWin("dataPanel", "editform", div, submitModify, panel +"--修改","保存提交");
		win.window({
			width:700,
			height:300,
			modal: true
		});
		repainLoc(record);
	} catch(e) {
		showJsException(e);
	}
};

var remove = function() {
	try{
		var rows = $('#dataPanel').datagrid("getSelections");
		if(rows.length == 0) {
			MyUtils.waring(_empty_select);
			return;
		}
		var ids = new Array();
		var names = new Array();
		for(var i = 0; i < rows.length; i++) {
			ids.push(rows[i].uuid);
			names.push(rows[i].brandName);
		}
		var data = { data:ids.toString()
			    ,st:tipc
			    ,msg:names.toString()
		};
		$.messager.confirm(_msg_sys,MyUtils.valueAlign(_remove_confirm,"left"), function(r) {
			if (r) {
				MyUtils.openMask(_msg_process,$('#t2'));
				MyUtils.sendPost("impl_remove/backRemove.adm",data,function(rets){
					try{
						MyUtils.hiddenMask($('#t2'));
						var res = MyUtils.toJsonObject(rets);
						if(res.error == '0') {
							MyUtils.info(res.msg);
							$('#dataPanel').datagrid("clearSelections");
							$('#dataPanel').datagrid("reload");
						} else {
							MyUtils.error(res.msg);
						}
					} catch (e) {
						showJsException(e);
					}
				});
			}
		});
		
	} catch(e) {
		showJsException(e);
	}
};

var dbclick = function(index,row){
	$('#dataPanel').datagrid("clearSelections");
	$('#dataPanel').datagrid("selectRow",index);
	modify();
};

var initPanel = function() {
	var columns = [
	               
	               {field : 'uuid',title : '(ID)编号',width : 150,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"left");}}
				    , {field : 'name',title : '位置名称',width : 150,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"left");}}
				    , {field : 'width',title : '宽度',width : 150,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"left");}}
				    , {field : 'height',title : '高度',width : 150,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"left");}}
				    ,{field : 'locDesc',title : '描述',width : 250,sortable:true,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"center");}}
				
					];
	var pam = {qr:"[]",st:tipc,sco:'',dc:Dmlx.sort.asc};
	MyUtils.createPaginPanelWithQueryparams('dataPanel',null,Dmlx._hFrozenColumns,columns,toolbars,dbclick, panel,null);
	$('#dataPanel').datagrid({
		url:"impl_search/commonsPaginSearch.ajax",
		queryParams:pam,
		pagination:true,
		fitColumns:true
	});
	
};

$(function(){
	try {
		initPanel();

	} catch (e) {
		showJsException(e,"数据加载失败!");
	}	
});

var submitAdd = function(){
	try {
		var dt = editValidator();
		if(dt != null) {
			MyUtils.openMask(_msg_process,win);
			$('#dt').val(dt);
			$('#dateEdit').attr("action",'impl_add/backAdd.adm');
			$('#dateEdit').submit();
		}
	} catch (e) {
		MyUtils.hiddenMask(win);
		showJsException(e,_msg_fail);
	}
};

var submitModify = function(){
	try {
		var dt = editValidator();
		if(dt != null) {
			$.messager.confirm(_msg_sys,MyUtils.valueAlign(_modify_confirm,"left"), function(r) {
				if(r) {
					MyUtils.openMask(_msg_process,win);
					$('#dt').val(dt);
					$('#dateEdit').attr("action",'impl_modify/backModify.adm?qy=');
					$('#dateEdit').submit();
					
				}
			});
		}
	} catch (e) {
		MyUtils.hiddenMask(win);
		showJsException(e,_msg_fail);
	}
};

var editValidator = function(){


	var record = {
			 uuid:$('#uuid').val()
			,name:$('#name').val()
			,width:$('#width').val()
			,height:$('#height').val()
			,locDesc:$('#locDesc').val()

		};
	
	//验证名称
	if(MyUtils.isEmpty(record.name)) {
		MyUtils.waring("广告位置名称不能为空");
		return null;
	}
	//验证宽高
	if(!MyUtils.isEmpty(record.width) && !MyUtils.isNumber(record.width)) {
		MyUtils.waring("广告位置宽度格式错误");
		return null;
	}
	//验证宽高
	if(!MyUtils.isEmpty(record.height) && !MyUtils.isNumber(record.height)) {
		MyUtils.waring("广告位置高度格式错误");
		return null;
	}
	return MyUtils.toJsonStr(record);
};



var getEditDiv = function(record) {
	var div= '<div id="editform"><iframe name="ifrName" src="" id="ifrName" style="display:none;"></iframe><form target="ifrName" action="" method="POST" name="fmName" id="dateEdit" >'
		+'<table cellspacing="2" cellpadding="2">'
		+'<tbody>'
		+'<tr>'
		+'<th align="right" width=200>广告位置名称：</th>'
		+'<td align="left" width=500>'
		+'<input type="hidden" id="uuid" value="" >'
		+'<input type="text" maxlength="120" id="name" style="width: 288px;" value=""><label class="requre_field">*</label>'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>广告位置宽度：</th>'
		+'<td align="left" width=500>'
		+'<input type="hidden" id="uuid" value="" >'
		+'<input type="text" maxlength="120" id="width" style="width: 288px;" value=""><label class="requre_field">*</label>'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>广告位置高度：</th>'
		+'<td align="left" width=500>'
		+'<input type="hidden" id="uuid" value="" >'
		+'<input type="text" maxlength="120" id="height" style="width: 288px;" value=""><label class="requre_field">*</label>'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" valign="top" width=200>位置描述：</th>'
		+'<td align="left"> <textarea rows="5"  id="locDesc" style="width: 500px;" ></textarea></td>'
		+'</tr>'
		+'</tbody>'
		+'</table>'
		+'<input type="hidden" id="dt" name="dt" value="" >'
		+'<input type="hidden" id="st" name="st" value="'+tipc+'" >'
		+'</form><div>';
	return div;
};


//赋值
var repainLoc = function(record){
	
	if(record) {
	
		$('#uuid').val(record.uuid);
		$('#name').val(record.name);
		$('#width').val(record.width);
		$('#height').val(record.height);
		$('#locDesc').val(record.locDesc);
	}
	$('#ifrName').load(function() {
		try {
			MyUtils.hiddenMask(win);
			 var response = this.contentWindow.document.body.textContent;
			 if(!MyUtils.isEmpty(response)){
				var res = MyUtils.toJsonObject(response);
				if(res.error == '0') {
					MyUtils.info(res.msg);
					if(win)win.window('close');
					$('#dataPanel').datagrid("clearSelections");
					$('#dataPanel').datagrid("reload");
				} else {
					MyUtils.error(res.msg);
				}
			 }
		} catch (e) {
			MyUtils.hiddenMask(win);
			showJsException(e,_msg_fail);
		}
	});
};

/**
 * 获取数字
 */
var getNumber=function(val) {
	return isNaN(val) ? 0 :val;
};

